package lfp.engine.utils;

/**
 * Created by IntelliJ IDEA.
 * User: zhangsiyuan
 * Date: 2006-11-30
 * Time: 15:34:07
 */
public class DataPretreatment {
    private static final double criticalNum = 100;

    public static double[] ptLoadArray(double[] src){
        int count = inValidPointCount(src);
        if(count == 0) return src;
        if(count > src.length / 3) return src;
        if(src[0] < criticalNum) {
            src[0] = getValidAvg(src);
            if(src[0] == 0.0)return null;
        }
        for(int i = 1; i < src.length; i++){
            if(src[i] < criticalNum) src[i] = src[i-1];
        }
        return src;
    }

    private static double getValidAvg(double[] src){
        double sum = 0;
        int count = 0;
        for(int i = 0; i < src.length; i++){
            if(src[i] > criticalNum){
                sum += src[i];
                count++;
            }
        }
        if(count == 0)return 0;
        return sum/count;
    }

    private static int inValidPointCount(double[] src){
        int count = 0;
        for(int i = 0; i < src.length; i++){
            if(src[i] < criticalNum){
                count++;
            }
        }
        return count;
    }
}
